package com.huang.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.entity.*;
import com.huang.eduservice.entity.frontVo.CourseFrontVo;
import com.huang.eduservice.entity.vo.CourseInfVo;
import com.huang.eduservice.entity.vo.CoursePublishVo;
import com.huang.eduservice.entity.vo.CourseWebVo;
import com.huang.eduservice.mapper.EduCourseMapper;
import com.huang.eduservice.service.EduChapterService;
import com.huang.eduservice.service.EduCourseDescriptionService;
import com.huang.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.eduservice.service.EduVideoService;
import com.huang.servicebase.execptionhandler.huangException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionService courseDescriptionService;
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    EduChapterService eduChapterService;



    @Override
    public String saveCourseVo(CourseInfVo courseInfVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfVo,eduCourse);
        //向课程表加数据
        baseMapper.insert(eduCourse);
       String id=eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription (courseInfVo.getDescription());
        eduCourseDescription.setId (id);

        courseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public CourseInfVo getCourseInfo(String courseId) {
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfVo courseInfVo = new CourseInfVo();

        BeanUtils.copyProperties(description,courseInfVo);
        BeanUtils.copyProperties(eduCourse,courseInfVo);
        return courseInfVo;
    }

    @Override
    public void updateCourseInfo(CourseInfVo courseInfVo) {
        EduCourse eduCourse = new EduCourse();

        EduCourseDescription description = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfVo,eduCourse);
        BeanUtils.copyProperties(courseInfVo,description);


        baseMapper.updateById(eduCourse);


        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishVo(String id) {
        CoursePublishVo publishVo = baseMapper.getPublishVo(id);
        return publishVo;
    }

    @Override
    public void removeCourse(String id) {

        //课程id删除小节

        eduVideoService.removeVideoByCourseId(id);

        //删除章节

        eduChapterService.removeChapterByCourseId(id);
        //删除描述
        courseDescriptionService.removeById(id);
        //删除课程
        int i = baseMapper.deleteById(id);
        if(i==0){
            throw new huangException(20001,"删除失败");
        }
    }

    @Override
    public List<EduCourse> getCouIndex() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 8");
        List<EduCourse> list = baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();

        long current = pageParam.getCurrent();

        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;

    }

    @Override
    public CourseWebVo getFrontCourseInfo(String courseId) {
        CourseWebVo courseWebVo=  baseMapper.getFrontCourseInfo(courseId);
        return courseWebVo;
    }


}
